package com.cjm.algorithm;

public class ListNodeHelp {
    public static ListNode toListNode(int[] nums) {
        ListNode head = null;
        ListNode cur = null;
        for (int i = 0; i < nums.length; i++) {
            if (cur == null) {
                cur = new ListNode();
                head = cur;
            }
            cur.val = nums[i];
            if (i != nums.length - 1) {
                ListNode temp = new ListNode();
                cur.next = temp;
                cur = temp;
            }
        }
        return head;
    }

    public static void printListNode(ListNode listNode) {
        if (listNode == null) {
            System.err.println("list node is empty");
        }
        ListNode cur = listNode;
        StringBuilder sb = new StringBuilder("");
        while (cur != null) {
            sb.append(cur.val).append(" -> ");
            cur = cur.next;
        }
        sb.append("null");
        System.err.println(sb.toString());
    }
}
